package com.chris.core.socket.netty.handler;

import com.chris.core.socket.netty.comm.ByteUtil;
import com.chris.core.socket.netty.dto.protocol.CmdObjectProtocol;
import com.chris.core.socket.netty.dto.protocol.CustomMessageProtocol;
import com.chris.core.socket.netty.dto.SocketParameterEnum;
import com.chris.core.socket.netty.dto.protocol.FileMessageProtocol;
import com.chris.core.socket.netty.util.NettyRunTimeUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义服务端事件
 */
public class CustomServerHandler extends ChannelInboundHandlerAdapter {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 当通道就绪
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    /**
     * 通道下线
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }


    /**
     * 读取发送数据
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof CustomMessageProtocol)
        {
            CustomMessageProtocol cmp =(CustomMessageProtocol)msg;
            if(cmp.getEnumType()== SocketParameterEnum.SystemCmdType) {
                log.debug("decode->cmd->" +  cmp.getEnumType()+ "->content:" + cmp.getObjectContent().toString());
            }
            else if(cmp.getEnumType()== SocketParameterEnum.StringType)
            {
                log.debug("decode->done->" +  cmp.getEnumType()+ "->content:" + cmp.getObjectContent().toString());
            }
            else if(cmp.getEnumType()== SocketParameterEnum.ObjectType)
            {
                log.debug("decode->done->" +  cmp.getEnumType()+ "->content:" + cmp.getObjectContent().toString());
            }
            else if(cmp.getEnumType()== SocketParameterEnum.SystemCmdObjectType)
            {
                CmdObjectProtocol p = (CmdObjectProtocol)ByteUtil.BytesToObject(cmp.getByteContent());

                log.debug("decode->cmdob->typemode->" +  p.getTypemode() +"->msg:" +p.getMsg());
            }
            else if(cmp.getEnumType()== SocketParameterEnum.FileObjectType)
            {
                // 重负载IO , 异步执行 read 内容, 此处是 只有一个线程运行，并发会排队

                ctx.channel().eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            NettyRunTimeUtil.doCustomTransfer(cmp, NettyRunTimeUtil.uploadFilePath, ctx.channel(),log);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
                //log.debug("decode->done->" + cmp.getEnumType()+ "->content:" + cmp.getObjectContent().toString());
            }
        }
        //如果不需要后续或最后一个，没有往下传递
        //super.channelRead(ctx, msg);
    }

    /**
     * 读取完毕
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }


    /**
     * 异常处理
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }



    /**
     * channel被加入事件
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        super.handlerAdded(ctx);
    }

    /**
     * channel被移除
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
    }
}
